package com.shrio.service;

import com.shrio.dao.MemberDao;
import com.shrio.entity.Member;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by luojie on 2018/3/27.
 */
@Repository
public class QueueListenter implements MessageListener {

    @Autowired
    private MemberDao memberDao;
    @Override
    public void onMessage(Message message) {
        try {
            System.out.println("-------QueueListenter--------");
            String data = new String(message.getBody(),"UTF-8");
            System.out.println(data);
            if(data != null){
                List<Member> list = memberDao.findAll();
                for(Member member : list){
                    member.setBalance(member.getBalance() + 10);
                    memberDao.update(member);
                }
                System.out.println("-------update success--------");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}
